package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xuanwei.core.entity.param.Comment;
import com.xuanwei.core.entity.vo.CommentStatistic;
import com.xuanwei.core.entity.vo.CommentVo;
import com.xuanwei.core.entity.vo.OrderCommentVo;
import com.xuanwei.core.entity.vo.ProductCommentVo;
import com.xuanwei.core.enums.CommentLevel;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwOrderCommentService;
import com.xuanwei.core.service.IXwOrderItemService;
import com.xuanwei.core.service.IXwOrderService;
import com.xuanwei.dao.entity.XwOrder;
import com.xuanwei.dao.entity.XwOrderComment;
import com.xuanwei.dao.entity.XwOrderItem;
import com.xuanwei.dao.mapper.XwOrderCommentMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-07-26
 */
@Service
public class XwOrderCommentServiceImpl extends ServiceImpl<XwOrderCommentMapper, XwOrderComment> implements IXwOrderCommentService {

    private IXwOrderItemService orderItemService;


    public XwOrderCommentServiceImpl(IXwOrderItemService orderItemService) {
        this.orderItemService = orderItemService;
    }

    @Override
    public ProductCommentVo findProductCommentVo(Integer productId) {

        XwOrderComment one = getOne(new QueryWrapper<XwOrderComment>()
                .eq("product_id", productId)
                .orderByDesc("create_time")
                .last("limit 1"));
        ProductCommentVo productCommentVo = new ProductCommentVo();
        if (one != null) {
            Integer levelCount = findLevelCount(productId, null);
            Integer levelCount1 = findLevelCount(productId, CommentLevel.LEVEL0.getCode());
            BigDecimal divide = new BigDecimal(levelCount1)
                    .multiply(new BigDecimal(100))
                    .divide(new BigDecimal(levelCount), 2, RoundingMode.UP);
            productCommentVo.setAll(levelCount);
            productCommentVo.setRate(divide.toString() + "%");
            productCommentVo.setContent(one.getComment());
            productCommentVo.setCreateTime(one.getCreateTime());
            productCommentVo.setImage(one.getImage());
            productCommentVo.setNickname(one.getUserNickname());
            productCommentVo.setSkuName(one.getSkuName());
            productCommentVo.setAvatar(one.getUserAvatar());
        } else {
            productCommentVo.setAll(0);
            productCommentVo.setRate("0.00%");
        }
        return productCommentVo;
    }

    @Override
    public CommentStatistic findCommentStatistic(Integer productId) {
        CommentStatistic commentStatistic = new CommentStatistic();
        Integer levelCount = findLevelCount(productId, null);
        Integer levelCount1 = findLevelCount(productId, CommentLevel.LEVEL0.getCode());
        Integer levelCount2 = findLevelCount(productId, CommentLevel.LEVEL1.getCode());
        Integer levelCount3 = findLevelCount(productId, CommentLevel.LEVEL2.getCode());
        QueryWrapper<XwOrderComment> wrapper = new QueryWrapper<XwOrderComment>()
                .eq("product_id", productId).ne("image", "").eq("product_id", productId);
        int count = count(wrapper);
        commentStatistic.setAll(levelCount);
        commentStatistic.setLevel0(levelCount1);
        commentStatistic.setLevel1(levelCount2);
        commentStatistic.setLevel2(levelCount3);
        if (levelCount != 0) {
            BigDecimal divide = new BigDecimal(levelCount1)
                    .multiply(new BigDecimal(100))
                    .divide(new BigDecimal(levelCount), 2, RoundingMode.UP);
            commentStatistic.setRate(divide.toString() + "%");
        } else {
            commentStatistic.setRate("0.00%");
        }

        commentStatistic.setImageNum(count);
        return commentStatistic;
    }

    @Override
    public List<CommentVo> findCommentList(Integer type, Integer productId, Page<XwOrderComment> page) {
        QueryWrapper<XwOrderComment> wrapper = new QueryWrapper<XwOrderComment>().eq("product_id", productId);
        if (type != null && type != 3) {
            wrapper.eq("level", type);
        }
        if (type != null && type == 3) {
            wrapper.ne("image", "");
        }
        wrapper.orderByDesc("create_time");
        page(page, wrapper);
        List<CommentVo> commentVos = Lists.newArrayListWithCapacity(page.getRecords().size());
        page.getRecords().forEach(s -> {
            CommentVo commentVo = new CommentVo();
            commentVo.setContent(s.getComment());
            commentVo.setId(s.getId());
            commentVo.setCreateTime(LocalDateTime.now());
            commentVo.setImage(s.getImage());
            commentVo.setNickname(s.getUserNickname());
            commentVo.setSkuName(s.getSkuName());
            commentVo.setAvatar(s.getUserAvatar());
            commentVos.add(commentVo);
        });
        return commentVos;
    }

    private Integer findLevelCount(Integer productId, Integer level) {
        QueryWrapper<XwOrderComment> wrapper = new QueryWrapper<XwOrderComment>().eq("product_id", productId);
        if (level != null) {
            wrapper.eq("level", level);
        }
        return count(wrapper);
    }


    @Override
    public OrderCommentVo findDetail(Integer orderId) {
        List<XwOrderComment> orderComments = list(new QueryWrapper<XwOrderComment>()
                .eq("order_id", orderId));
        if (orderComments == null || orderComments.size() == 0) {
            throw new XwException("订单数据错误");
        }

        OrderCommentVo orderCommentVo = new OrderCommentVo();
        List<OrderCommentVo.Item> items = Lists.newArrayListWithCapacity(orderComments.size());
        orderComments.forEach(orderComment -> {
            OrderCommentVo.Item item = new OrderCommentVo.Item();
            Comment.CommentItem commentItem = new Comment.CommentItem();
            commentItem.setContent(orderComment.getComment());
            commentItem.setImage(orderComment.getImage());
            commentItem.setLevel(orderComment.getLevel());
            item.setCommentItem(commentItem);
            Integer orderItemId = orderComment.getOrderItemId();
            XwOrderItem orderItem = orderItemService.getById(orderItemId);
            item.setImage(orderItem.getImage());
            item.setProductName(orderItem.getProductName());
            item.setSkuName(orderItem.getSkuName());
            items.add(item);
        });
        orderCommentVo.setItems(items);
        return orderCommentVo;
    }
}
